<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2021.3.23
 * Time: 14:06
 */

class Solution
{
    public function singleNumber($array) {
        $arrLength = count($array);

        if($arrLength == 0) {
            return 0;
        }

        if($arrLength == 1) {
            return $array[0];
        }

        $newArr = [];
        for ($i=0;$i<$arrLength;$i++) {
            if(array_key_exists($array[$i],$newArr)) {
                ++$newArr[$array[$i]];
            } else {
                $newArr[$array[$i]] = 1;
            }
        }

        for ($i=0;$i<$arrLength;$i++) {
            if(array_key_exists($array[$i],$newArr) && $newArr[$array[$i]] == 1) {
                return $array[$i];
            }
        }
        return 0;
    }
}

$array = [2,2,1];
$solution = new Solution();
$res = $solution->singleNumber($array);
var_dump($res);